﻿package src.uipackage.ui.graphics {
	
	import caurina.transitions.Tweener;
	import com.onebyonedesign.ui.complexbuttons.events.ComplexButtonEvent;
	import com.onebyonedesign.ui.complexbuttons.IComplexButton;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	* Individual sections of OBO_Accordion. Uses Accordion button for hitarea.
	* 
	* REQUIRED: caurina.transitions.Tweener
	* 
	* @author Devon O.
	*/
	public class AccordionSection extends Sprite implements IComplexButton {
		
		private var _mask:Sprite;
		private var _content:DisplayObject;
		private var _btn:AccordionButton;
		private var _format:TextFormat;
		private var _tf:TextField;
		private var _label:String;
		
		private var _data:String;
		private var _contentHeight:Number;
		private var _btnHeight:Number;
		
		private var _open:Boolean = false;
		
		public function AccordionSection(label:String, content:DisplayObject, font:String, fontSize:int, fontColor:uint, height:Number, topGradient:uint, bottomGradient:uint, btnWidth:Number, btnHeight:Number, useRoundedCorners:Boolean):void {
			
			_contentHeight = height;
			_label = label;
			_content = content;
			_btnHeight = btnHeight;
			
			_format = new TextFormat(font, fontSize, fontColor);
			
			_btn = new AccordionButton(topGradient, bottomGradient, btnWidth, btnHeight, useRoundedCorners);
			
			_mask = new Sprite();
			_mask.graphics.beginFill(0x000000);
			_mask.graphics.drawRect(0, 0, btnWidth, height);
			_mask.graphics.endFill();
			_mask.mouseEnabled = false;
			
			init();
		}
		
		private function init():void {
			_mask.height = 0;
			hitArea = _btn;
			
			_tf = new TextField();
			_tf.autoSize = TextFieldAutoSize.LEFT;
			_tf.antiAliasType = AntiAliasType.ADVANCED;
			_tf.selectable = false;
			_tf.mouseEnabled = false;
			_tf.defaultTextFormat = _format;
			_tf.text = _label;
			_tf.x = 10;
			_tf.y = Math.round((_btn.height * .5) - (_tf.height * .5));
			_btn.addChild(_tf);
			
			_content.mask = _mask;
			_mask.y = _btnHeight;
			_content.y = _mask.y
			addChild(_btn);
			addChild(_mask);
			addChild(_content);
		}
		
		public function displayOver():void {
			_btn.showOver();
		}
		
		public function displayDown():void {
			_btn.showDown();
			_open = true;
			Tweener.addTween(_mask, { height:_contentHeight, transition:"easeInQuad", onUpdate:dispatchAccEvent, time:.4 } );
		}
		
		public function displayUp():void {
			_btn.showUp();
			if (_open) {
				Tweener.addTween(_mask, { height:0, transition:"easeInQuad", onUpdate:dispatchAccEvent, time:.4 } );
				_open = false;
			}
		}
		
		private function dispatchAccEvent():void {
			dispatchEvent(new ComplexButtonEvent(ComplexButtonEvent.ACCORDION_CHANGE));
		}
		
		public function get data():String { return _data; }
		
		public function set data(data:String):void { _data = data; }
		
		public function get contentHeight():Number { return _mask.height; }
		
		public function get content():DisplayObject { return _content; }
	}
	
}